package edu.cmu.javadb.plan.condition;

import java.util.*;

import edu.cmu.javadb.Attribute;
import edu.cmu.javadb.RecordInfo;
import edu.cmu.javadb.exceptions.NoAttributeException;

public class In extends Condition {

	@Override
	public boolean satisfy(Object o) {
		try {
			Attribute a, b;
			Object v1;
			Collection v2;
			v1 = (Comparable) getValue(o, left);
			if (v1 == null) {
				return false;
			}

			v2 = (Collection) getValue(o, right);
			if (v2 == null) {
				return false;
			}
			return v2.contains(v1);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		return false;
	}

	private Object left;
	private Object right;

	public In(Object attribute1, Object attribute2) {
		this.left = attribute1;
		this.right = attribute2;
	}

	@Override
	public boolean containRootPath() {
		if (left instanceof Path) {
			return ((Path) left).isRoot();
		}
		return false;
	}

	@Override
	public List<Attribute> getAllFields(RecordInfo recInfo) {

		List<Attribute> res = new ArrayList<Attribute>();
		if (left instanceof Path) {
			try {
				if (! ((Path) left).isRoot())
					res.add(recInfo.getAttribute( ((Path) left).getPath()));
			} catch (NoAttributeException e) {
				e.printStackTrace();
			}
		}
		return res;

	}

	@Override
	public boolean containNonValBinary() {
		//TODO
		return true;
	}
}
